﻿var roleedit = {
    formDefine: {
        labelWidth: 75, inputWidth: 240, space: 10, labelAlign: 'right',
        fields: [
                    { display:lang.group_name+"：", name: "ROLES_NAME", type: "text", newline: true, width: 280, serviceDataName: 'ROLES_NAME' },
                    { display: lang.parent_node_locaion+"：", name: "PARENTID", type: "selecttree", inputWidth: 238, newline: true, dataoptions: "panelHeight:170" },
                    { display: lang.sequence+"：", name: "SORTCODE", type: "text", newline: true, serviceDataName: 'SORTCODE' },
                    { display: lang.group_description+"：", name: "ROLES_REMARK", type: "textarea", newline: true, width: 280, serviceDataName: 'ROLES_REMARK' },
                    { display: lang.save, name: "search", type: "button", newline: true, nlstyle: "text-align:center;padding-top:15px;", width: 280, click: function () { roleedit.form.submit(); } },
                    { display: lang.close, name: "close", type: "button", width: 280, click: function () {
                        parent.$.closePage();
                    } 
                    }
                ],
        onLoaded: function (data, form) {
            var _currentval = '';
            if (data && data.PARENTID != '') {
                _currentval = data.PARENTID;

            }
            loadRoleData(_currentval);
        },
        submitUrl: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        submitUrlType: "EditRole",
        submitCheck: function (params) {
            if (!params["ROLES_NAME"] || params["ROLES_NAME"] == "") {
                return lang.group_name_cannot_be_empty;
            }
            if (!params["PARENTID"] || params["PARENTID"] == "") {
                return lang.node_location_cannot_be_empty;
            }
            if (!params["SORTCODE"] || params["SORTCODE"] == "") {
                return lang.sequence_cannot_be_empty;
            }
            if (isNaN(params["SORTCODE"])) {
                return lang.sequence_must_be_digital;
            }
            return "";
        },
        submitParams: function (params) {

            if ($.getPara('roleid') && $.getPara('roleid') != '') {
                params.action = 1;
                params.ROLES_ID = $.getPara('roleid');
            } else { params.action = 2; }
            return params;
        },
        submitSuccess: function () {
            if ($.getPara('roleid') && $.getPara('roleid') != '') {
                $.alert(lang.role_updated_successfully, null, function () { parent.rolelist.search(); parent.$.closePage(); });
            } else {
                $.alert(lang.add_role_success, null, function () { parent.rolelist.search(); parent.$.closePage(); });
            }
         }
    },
    init: function () {
        if ($.getPara('roleid') != '') {
            this.formDefine.url = App_Config.TadongUrl + "/AjaxMappingHandler.aspx";
            this.formDefine.urlType = "GetRolesByID";
            this.formDefine.params = function () { return $.getPara('roleid'); };
        }
        jsPageRender(this);
    }
};

//#region 加载角色数据
function loadRoleData(_currentval) {
    $.ajaxHandler({
        url: App_Config.TadongUrl + "/AjaxMappingHandler.aspx",
        urlType: "RolesList",
        success: function (data) {
            if (data && data.length > 0) {
                var _data = [];
                var _dataarr = [];
                var rootobj = {};
                rootobj.id = 0;
                rootobj.text = lang.root;
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PARENTID == '0') {
                        var obj = {};
                        obj.id = data[i].ROLES_ID;
                        obj.text = data[i].ROLES_NAME;
                        obj.SORTCODE = data[i].SORTCODE;
                        obj.CREATEUSERNAME = data[i].CREATEUSERNAME;
                        obj.CREATEDATE = data[i].CREATEDATE;
                        obj.MODIFYUSERNAME = data[i].MODIFYUSERNAME;
                        obj.MODIFYDATE = data[i].MODIFYDATE
                        obj.ROLE_RESTRICTION = data[i].ROLE_RESTRICTION;
                        obj.DELETEMARK = data[i].DELETEMARK;
                        getsubRole(data, data[i].ROLES_ID, obj);
                        _data.push(obj);
                    }
                }
                rootobj.children = _data;
                _dataarr.push(rootobj);
                $('#formidPARENTID').combotree({ multiple: false });
                $('#formidPARENTID').combotree('loadData', _dataarr);
                if (_currentval != '') {
                    $('#formidPARENTID').combotree('setValue', _currentval);
                }
            }

        }
    });

    function getsubRole(_d, _pid, obj2) {
        if (_d && _d.length > 0) {
            var _sdata = [];
            for (var i = 0; i < _d.length; i++) {
                if (_d[i].PARENTID == _pid) {
                    var obj = {};
                    obj.id = _d[i].ROLES_ID;
                    obj.DELETEMARK = _d[i].DELETEMARK;
                    obj.text = _d[i].ROLES_NAME;
                    obj.ROLE_RESTRICTION = _d[i].ROLE_RESTRICTION;
                    obj.CREATEUSERNAME = _d[i].CREATEUSERNAME;
                    obj.CREATEDATE = _d[i].CREATEDATE;
                    obj.MODIFYUSERNAME = _d[i].MODIFYUSERNAME;
                    obj.MODIFYDATE = _d[i].MODIFYDATE
                    obj.SORT_CODE = _d[i].SORT_CODE;
                    _sdata.push(obj);
                }
            }
            obj2.children = _sdata;
            if (_sdata.length > 0) {
                for (var j = 0; j < _sdata.length; j++) {
                    getsubRole(_d, _sdata[j].ROLES_ID, _sdata[j]);
                }
            }
        }
    }
}

//#endregion